<template>
	<view>
		<view class="cu-form-group">
			<view class="title">兽药类型</view>
			<picker @change="ywChange" :value="ywIndex" :range="ywShow">
				<view class="picker">
					{{ywShow[ywIndex]}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">使用对象</view>
			<picker @change="hzChange" :value="hzIndex" :range="hzShow">
				<view class="picker">
					{{hzShow[hzIndex]}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">兽药名称</view>
			<input v-model="purchasePharmacy" placeholder="请输入兽药名称" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">采购人</view>
			<input v-model="purchasePeople" placeholder="请输入采购人" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">采购人手机</view>
			<input v-model="purchasePhone" placeholder="请输入采购人手机号码" maxlength="11" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">药品供应商</view>
			<input v-model="purchaseMerchant" placeholder="请输入药品供应商" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">供应商电话</view>
			<input v-model="purchaseMerchantPhone" placeholder="请输入供应商电话" maxlength="11"  name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">购入数量/瓶</view>
			<input v-model="purchaseNumber" placeholder="请输入购入数量/瓶" type="number" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">购入日期</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
				<view class="picker">{{date}}</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">花费金额/元</view>
			<input v-model="purchaseMoney" placeholder="请输入金额" type="digit" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">兽药过期日期</view>
			<picker mode="date" :value="date1" :start="startDate1" :end="endDate1" @change="bindDateChange1">
				<view class="picker">{{date1}}</view>
			</picker>
		</view>
	</view>
</template>

<script>
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
	function getDate1(type) {
		const date1 = new Date();
	
		let year = date1.getFullYear();
		let month = date1.getMonth() + 1;
		let day = date1.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
	export default {
		data() {
			return {
				ywShow:['一类','二类'],
				useType:[{
					id:0
				},{
					id:1
				}],
				ywIndex:0,
				hzShow:[],
				hzIndex:0,
				type:"",
				date: getDate({
					format: true
				}),//采购时间
				startDate:getDate('start'),
				endDate:getDate('end'),
				date1: getDate1({
					format: true
				}),//过期时间
				startDate1:getDate1('start'),
				endDate1:getDate1('end'),
				
				purchaseMerchant:'',//药品供应商
				purchaseMerchantPhone:"",//供应商电话
				purchaseMoney:'',//采购金额
				purchaseNumber:"",//购入数量
				purchasePeople:'',//采购人
				purchasePharmacy:'',//药品名称
				purchasePhone:'',//采购人联系方式
			}
		},
		onNavigationBarButtonTap() {
			uni.showModal({
				title:"提示",
				content:"确定要提交此次记录吗？",
				success: (res) => {
					if(res.confirm){
						this.addRecord();
					}	
				}
			})
		},
		onReady() {
			this.getType()
		},
		methods: {
			addRecord(){
				console.log(this.useType[this.ywIndex].id);
				console.log(this.type[this.hzIndex].type_id);
				uni.request({
					url:this.confige.webUrl+"/livestock/addMedicationStock",
					method:"POST",
					header:{
						'X-Token':this.User.token
					},
					data:{
						farm_id:this.User.userInfo.farm_id,
						purchaseDrugType:this.useType[this.ywIndex].id,
						purchaseMerchant:this.purchaseMerchant,
						purchaseMerchantPhone:this.purchaseMerchantPhone,
						purchaseMoney:this.purchaseMoney,
						purchaseNumber:this.purchaseNumber,
						purchaseOverdue:this.date1,
						purchasePeople:this.purchasePeople,
						purchasePharmacy:this.purchasePharmacy,
						purchasePhone:this.purchasePhone,
						purchaseTime:this.date,
						purchaseTypeId:this.type[this.hzIndex].type_id
					},
					success: (res) => {
							console.log(res)
							if(res.data.code==200){
								uni.showToast({
									title:res.data.message
								})
								uni.switchTab({
									url:"../rear"
								})
							}
					}
				})
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
				console.log(this.date)
			},
			bindDateChange1: function(e) {
				this.date1 = e.detail.value
				console.log(this.date1)
			},
			//获取种类
			getType(){
				uni.request({
					url:"http://222.217.160.124:8999/Intelligent_breeding/get_alltype",
					success: (res) => {
						console.log(res)
						this.type = res.data.message;
						let arr = [];
						for (var i = 0; i < res.data.message.length; i++) {
							arr.push(this.type[i].type_name)
						};
						this.hzShow = arr;
					}
				})
			},
			//水体异味选中
			ywChange(e){
				this.ywIndex = e.detail.value;
			},
			//水体浑浊度
			hzChange(e){
				this.hzIndex = e.detail.value;
			},
			
			
		}
	}
</script>

<style lang="less" >
	@import '../../../common/animate.css';
	@import '../../../common/main.css';
	@import "../../../common/iconfont.css";
	@import "../../../common/colorui/icon.css";
	
</style>
